package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/micro/go-micro/v2/logger"
	"go-micro/src/Boot"
	"go-micro/src/Config"
	"os"
	"os/signal"
	"syscall"
)


func CheckForReady() gin.HandlerFunc {
	return func(i *gin.Context) {
		if !Boot.ServerIsReady() {
			i.AbortWithStatusJSON(200, gin.H{"result": "server is loading"})
		} else {
			i.Next()
		}
	}
}

func main() {
	Boot.BootInit()
	router := gin.New()
	router.Use(CheckForReady())
	router.Handle("GET", "/test", func(context *gin.Context) {
		context.JSON(200, gin.H{"result": Config.JConfig.Data.Mysql})
	})

	go func() {
		err := router.Run(":1234")
		if err != nil {
			Boot.BootErrChan <- err
		}
	}()
	go func() {
		sig_c := make(chan os.Signal)
		signal.Notify(sig_c, syscall.SIGINT, syscall.SIGTERM)
		Boot.BootErrChan <- fmt.Errorf("%s", <-sig_c)
	}()
	getErr := <-Boot.BootErrChan
	logger.Info(getErr)
}
